const Sprite = require('./Sprite');
/**
 * @class 角色
 */
class Character extends Sprite {
    /**
     * @constructor
     * @param {Object} imgs - 图像
     * @param {Object} icon - 头像/半身像,参数是图片则为图片，参数为xywh则从图像中截取
     */
    constructor(name, imgs, icon, paint, behavior) {
        super(name, paint, behavior);
        this.imgs = imgs;
        this.icon = icon;
        /**昵称 */
        this.nickname = '';
        /**称号/别称/外号 */
        this.tags = '江湖虾米';
        /**性别 */
        this.sex = '';
        /**性取向 */
        this.sexual = '';
        /**年龄 */
        this.age = 0;
        /**阵营 */
        this.camp = '';
        /**道德值 */
        this.morality = 0;
        /**地址 */
        this.address = {
            country: '',
            city: '',
        };
        /**等级 */
        this.level = 0;
        /**当前经验值 */
        this.xp = 0;
        /**下一等级所需经验值 */
        /**属性点 */
        this.attrPoint = 0;
        this.nextXp = 0;
        /**人物描述/传记 */
        this.biography = '';
        /**携带物品 */
        this.goods = {};
        /**携带物品种类上限数 */
        this.bagMax = 40;
        /**道具栏物品 */
        this.prop = {};
        /**拥有的金钱 */
        this.money = 0;
        /**角色 */
        this.roles = [];
        /**身法 */
        this._agile = 0;
        /**体质 */
        this._fitness = 0;
        /**魅力 */
        this._charm = 0;
        /**感知/探索能力 */
        this._perception = 0;
        /**拳掌法 */
        this._boxing = 0;
        /**剑法 */
        this._sword = 0;
        /**刀法 */
        this._kinfe = 0;
        /**奇门/暗器 */
        this._hiddenWeapon = 0;
        /**臂力 */
        this._strength = 0;
        /**悟性 */
        this._understanding = 0;
        /**根骨 */
        this._bone = 0;
        /**定力 */
        this._ability = 0;
        /**福源 */
        this._lucky = 0;
        /**速度 */
        this.speed = 0;
        /**移动范围 */
        this.range = 0;
        /**全部生命值 */
        this.lifefull = 0;
        /**当前生命值 */
        this.lifecurr = 0;
        /**装备等相加综合计算的攻击力 */
        this.attack = [0, 0];
        /**装备等相加综合计算的防御力 */
        this.defense = [0, 0];
        /**基础攻击力 */
        this._attack = [0, 0];
        /**基础防御力 */
        this._defense = [0, 0];
        /**载重 heavy已载重, total总共可载重*/
        this.carrying = {
            heavy: 0,
            total: 0,
        };
        /**技能 */
        this.skill = {};
        /**心法、内功 */
        this.heart = {};
        /**正面效果 */
        this.buff = [];
        /**负面效果 */
        this.debuff = [];
        /**状态 - 受伤 死亡 残疾 健康 */
        this.state = '';
        /**装备 Object,初始赋值undefined*/
        this.equipment = {
            /**头部装备 */
            head: undefined,
            /**身体 */
            body: undefined,
            /**脚部 */
            foot: undefined,
            /**武器 */
            arms: undefined,
            /**饰品 */
            ornament: undefined,
        };
        /**关系圈 - name: {people: Object人, favor: '好感度' , tags: ['关系标签']} */
        this.relationship = {};
        /**队伍 */
        this.team = [undefined, undefined, undefined, undefined, undefined];
        /**天赋 */
        this.special = {};
    }

    /**
     * 添加物品
     * @param {Object} good - 物品对象
     * @param {Number} num - 数量 - 可选默认1
     */
    addGoods(good, num = 1) {
        let keys = Object.keys(this.goods);
        if (keys.length >= this.bagMax) {
            return this;
        }
        if (keys.length == 0) {
            this.goods[good.key] = { good, index: 0, num };
        } else {
            for (let i = 0; i < keys.length; i++) {
                let key = keys[i];
                let tmp = this.goods[key];
                if (good.key == tmp.good.key) {
                    tmp.num += num;
                } else {
                    this.goods[good.key] = { good, index: keys.length - 1, num };
                }
            }
        }
        this._autoWeight();
        return this;
    }

    /**
     * 
     * @param {Object} good - 物品
     * @param {Number} num - 数量 - 可选默认1
     * @param {Boolean} issale - 是贩卖还是丢弃，默认丢弃false，贩卖true
     */
    removeGoods(good, num = 1, issale = false) {
        let tmp = this.goods[good.key];
        num = num > tmp.num ? tmp.num : num;
        tmp.num -= num;
        if (tmp.num <= 0) {
            delete this.goods[good.key];
        }
        if (issale) {
            this.money += tmp.sale;
        }
        this._autoWeight();
        return this;
    }

    /**
     * 穿上装备
     * @param {String} key - 装备英文名称/key
     * @param {String} position - 可选参数：装备部位名称，只有在戒指、双手武器时需要指定
     */
    putEquipment(key, position) {
        let good = this.goods[key].good;
        if (!good instanceof Equipment) {
            return this;
        }
        let pos = position ? position : good.position;
        // if (['armsL', 'armsR'].includes(position) && good.position == 'armsTwo') {
        //     this.unEquipment('armsL');
        //     this.unEquipment('armsR');
        // }
        // if (['armsL', 'armsR'].includes(good.position)) {
        //     if (this.equipment.armsL.position == 'armsTwo') {
        //         this.unEquipment('armsL');
        //     }
        //     if (this.equipment.armsR.position == 'armsTwo') {
        //         this.unEquipment('armsR');
        //     }
        // }
        this.unEquipment[pos];
        this.equipment[pos] = good;
        this.removeGoods(good, 1);
        this._autoAttr();
        return this;
    }

    /**
     * 去除装备
     * @param {String} position - 装备位置
     */
    unEquipment(position) {
        if (!this.equipment[position]) {
            return this;
        }
        let good = this.equipment[position];
        this.equipment[position] = undefined;
        this.addGoods(good);
        this._autoAttr();
        return this;
    }

    /**
     * 升级
     */
    levelUp() {
        this.level += 1;
        this.attrPoint += 0; // 暂定
        this.xp = this.xp - this.nextXp;
        this.nextXp = 0; // 暂定
    }

    /**
     * 计算携带物品重量
     */
    _autoWeight() {
        let num = 0;
        let keys = Object.keys(this.goods);
        for (let i = 0; i < keys.length; i++) {
            let key = keys[i];
            let good = this.goods[key].good;
            num += good.weight * this.goods[key].num;
        }

        let eqs = Object.keys(this.equipment);
        for (let i = 0; i < eqs.length; i++) {
            let good = this.equipment[eqs[i]];
            if (good) {
                num += good.weight;
            }
        }
        this.carrying.heavy = num;
        return this;
    }

    /**
     * 计算攻击和防御力和其他属性
     */
    _autoAttr() {
        this.attack[0] = this._attack[0];
        this.attack[1] = this._attack[1];
        this.defense[0] = this._defense[0];
        this.defense[1] = this._defense[1];
        let keys = Object.keys(this.equipment);
        for (let i = 0; i < keys.length; i++) {
            let key = keys[i];
            let eq = this.equipment[key];
            if (eq) {
                this.attack[0] += this._attack[0] + eq.attack[0];
                this.attack[1] += this._attack[1] + eq.attack[1];
                this.defense[0] += this._defense[0] + eq.defense[0];
                this.defense[1] += this._defense[1] + eq.defense[1];
            }
        }
        return this;
    }


    /**身法 */
    set agile(val) {
        this._agile = val;
    }
    /**体质 */
    set fitness(val) {
        this._fitness = val;
    }
    /**魅力 */
    set charm(val) {
        this._charm = val;
    }
    /**感知/探索能力 */
    set perception(val) {
        this._perception = val;
    }
    /**拳掌法 */
    set boxing(val) {
        this._boxing = val;
    }
    /**剑法 */
    set sword(val) {
        this._sword = val;
    }
    /**刀法 */
    set kinfe(val) {
        this._kinfe = val;
    }
    /**奇门/暗器 */
    set hiddenWeapon(val) {
        this._hiddenWeapon = val;
    }
    /**臂力 */
    set strength(val) {
        this._strength = val;
    }
    /**悟性 */
    set understanding(val) {
        this._understanding = val;
    }
    /**根骨 */
    set bone(val) {
        this._bone = val;
    }
    /**定力 */
    set ability(val) {
        this._ability = val;
    }
    /**福源 */
    set lucky(val) {
        this._lucky = val;
    }

    /**身法 */
    get agile() {
        return this._agile;
    }
    /**体质 */
    get fitness() {
        return this._fitness;
    }
    /**魅力 */
    get charm() {
        return this._charm;
    }
    /**感知/探索能力 */
    get perception() {
        return this._perception;
    }
    /**拳掌法 */
    get boxing() {
        this._boxing = val;
    }
    /**剑法 */
    get sword() {
        return this._sword;
    }
    /**刀法 */
    get kinfe() {
        return this._kinfe;
    }
    /**奇门/暗器 */
    get hiddenWeapon() {
        return this._hiddenWeapon;
    }
    /**臂力 */
    get strength() {
        return this._strength;
    }
    /**悟性 */
    get understanding() {
        return this._understanding;
    }
    /**根骨 */
    get bone() {
        return this._bone;
    }
    /**定力 */
    get ability() {
        return this._ability;
    }
    /**福源 */
    get lucky() {
        return this._lucky;
    }
}

module.exports = Character;